//# sourceURL=http://uMapApp/UI/Layer
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../Earth/DataSourceView/C3DTilePrimitiveDataSourceView",
    "../Earth/DataSourceView/UrlTemplateImageryDataSourceView",
    "../Earth/DataSourceView/WMTSImageryDataSourceView",
    "../Earth/DataSourceView/GLTFDataSourceView",
    "../Earth/DataSourceView/GEOJSONDataSourceView",
    "../Earth/DataSourceView/CesiumTerrainDataSourceView",
    "../Earth/ViewPort"
], function (
    mhjs,
    uMap,
    uEarth,
    C3DTilePrimitiveDataSourceView,
    UrlTemplateImageryDataSourceView,
    WMTSImageryDataSourceView,
    GLTFDataSourceView,
    GEOJSONDataSourceView,
    CesiumTerrainDataSourceView,
    ViewPort
) {

    function Layer(option) {

        var _this = this;

        _this.app = option.app;
        _this.dataSources = new uEarth.CustomDataSource(_this.title);
        _this.primitives = new uEarth.PrimitiveCollection();
        _this.dataSources.show = false;
        _this.primitives.show = false;
        _this.app.earth.dataSources.add(_this.dataSources);
        _this.app.earth.scene.primitives.add(_this.primitives);
        _this.layers = [];

        mhjs.Div.call(_this, option);
        _this.addClass("layer");
    }

    var _class = Layer;

    mhjs.Inherit(_class, mhjs.Div);

    Object.defineProperties(_class.prototype, {
        "title": {
            get: function () {
                return this._title;
            },
            set: function (value) {
                this._title = value;
            }
        },
        "viewport": {
            get: function () {
                return this._viewport;
            },
            set: function (value) {
                this._viewport = new ViewPort(value);
            }
        },
        "layers": {
            get: function () {
                return this._layers;
            },
            set: function (value) {

                var _this = this;

                if (value) {
                    _this._layers = [];
                    $.each(value, function (i, item) {
                        _this.addLayer(item);
                    });
                }
            }
        },
        "dataSource": {
            get: function () {
                return this._dataSource;
            },
            set: function (dataSource) {

                var _this = this;

                dataSource.layer = _this;

                if (dataSource.viewport) {
                    _this.viewport = _this.viewport || dataSource.viewport;
                }

                if (dataSource.type === "C3DTilePrimitiveDataSource") {

                    _this.dataSourceView = new C3DTilePrimitiveDataSourceView(dataSource);
                    _this.primitives.add(_this.dataSourceView.tileSet.object);

                    _this.dataSourceView.on("loaded", function () {
                        _this.viewport = _this.viewport || _this.dataSourceView.viewport;
                    });

                    _this.dataSourceView.close();


                } else if (dataSource.type === "UrlTemplateImageryDataSource") {

                    _this.dataSourceView = new UrlTemplateImageryDataSourceView(dataSource);
                    _this.app.earth.imageryLayers.add(_this.dataSourceView.imagery.object);
                    _this.dataSourceView.close();

                } else if (dataSource.type === "WMTSImageryDataSource") {

                    _this.dataSourceView = new WMTSImageryDataSourceView(dataSource);
                    _this.app.earth.imageryLayers.add(_this.dataSourceView.imagery.object);
                    _this.dataSourceView.close();

                } else if (dataSource.type === "GLTFDataSourceView") {

                    _this.dataSourceView = new GLTFDataSourceView(dataSource);
                    _this.primitives.add(_this.dataSourceView.model.object);

                    _this.dataSourceView.model.on("loaded", function (model) {

                        _this.viewport = _this.viewport || {
                            "center": model.transform.position.value,
                            "hpr": {
                                "heading": 0,
                                "pitch": -30,
                                "range": model.boundingSphere.radius
                            }
                        };

                    });

                    _this.dataSourceView.close();

                } else if (dataSource.type === "GEOJSONDataSourceView") {
                    _this.dataSourceView = new GEOJSONDataSourceView(dataSource);
                    _this.dataSources.add(_this.dataSourceView.dataSource.object);

                    _this.dataSourceView.close();
                } else if (dataSource.type === "CesiumTerrainDataSource") {
                    _this.dataSourceView = new CesiumTerrainDataSourceView(dataSource);
                    _this.dataSourceView.close();
                }

                if (dataSource.list) {
                    $.each(dataSource.list, function (i, item) {
                        _this.addLayer({
                            "dataSource": item
                        });
                    });
                }

                _this._dataSource = dataSource;

            }
        },
        "addLayer": {
            value: function (layer) {
                var _this = this;

                if (layer.constructor.name === "Layer") {
                    _this.layers.push(layer);
                } else if (layer.constructor.name === "Object") {
                    layer.app = _this.app;
                    layer.parentLayer = _this;
                    _this.addLayer(new Layer(layer));
                }
            }
        },
        "open": {
            value: function () {

                var _this = this;

                if (!_this.isOpened) {

                    _this.show();
                    _this.dataSources.show = true;
                    _this.primitives.show = true;

                    if (_this.dataSourceView) {
                        _this.dataSourceView.open();
                    }

                    if (_this.layers && _this.layers.length > 0) {
                        $.each(_this.layers, function (i, layer) {
                            layer.open();
                        });
                    }

                    this.isOpened = true;
                    this.trigger("open");
                    this.trigger("toggle");
                }
            }
        },
        "close": {
            value: function () {

                var _this = this;

                if (this.isOpened) {

                    _this.hide();
                    _this.dataSources.show = false;
                    _this.primitives.show = false;

                    if (_this.dataSourceView) {
                        _this.dataSourceView.close();
                    }

                    if (_this.layers && _this.layers.length > 0) {
                        $.each(_this.layers, function (i, layer) {
                            layer.close();
                        });
                    }

                    this.isOpened = false;
                    this.trigger("close");
                    this.trigger("toggle");
                }
            }
        },
        "toggle": {
            value: function (value) {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "remove": {
            value: function () {
                var _this = this;
                _this.app.earth.dataSources.remove(_this.dataSources);
            }
        },
        "active": {
            value: function () {

                var _this = this;

                _this.open();


                if (!_this.viewport) {

                    if (_this.dataSource.type === "C3DTilePrimitiveDataSource") {
                        _this.dataSourceView.tileSet.on("loaded", function (model) {
                            _this.flyVP();
                        });
                    }

                } else {
                    _this.flyVP();
                }

                _this.trigger("active");

            }
        },
        "flyVP": {
            value: function () {

                var _this = this;

                _this.app.earth.camera.flyToBoundingSphere(
                    _this.viewport.boundingSphere,
                    {
                        offset: _this.viewport.hpr
                    });
            }
        }
    });

    return _class;

});